
use std::{io, cmp::Ordering};
use rand::prelude::*;
/*猜数游戏*/
pub fn guess_num(){
    println!("猜数!");
    let mut rng: ThreadRng = rand::thread_rng();

    let  secret_num: u8  = rng.gen();

    println!("神秘数字:{}", secret_num);

    let mut foo = 1; //可变变量

    let bar = foo;

    foo = 2;

    println!("{}, {}", foo, bar);

    let mut guess_num: u8;
    //循环
    loop{
        let mut guess_str: String = String::new(); //创建空白字符串

        //& 引用 &mut 可变引用
        io::stdin().read_line(&mut guess_str).expect("无法读取行");

        //同名旧变量被隐藏  guess.trim()去掉空白， parse()：解析成数字
        guess_num = match guess_str.trim().parse() {
            Ok(num) => num,
            Err(_) =>continue,
        };

        match guess_num.cmp(& secret_num){
            Ordering::Less => println!("Too small"),
            Ordering::Greater => println!("Too big"),
            Ordering::Equal => {
                println!("You win");
                break;
            },
        };
    }
}